import { login, logout, getInfo } from '@/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import router, { resetRouter } from '@/router'

const state = {
  token: getToken(),
  name: '',
  account: '',
  depart: '',
  id_depart: 0,
  role: '',
  id_role: 0,
  uid: 0,
  index_column: [],
  avatar: '',
  introduction: '',
  roles: [],
  userInfo:''
}

const mutations = {
  SET_USERINFO:(state,userInfo)=>{
    state.userInfo
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  SET_ACCOUNT: (state, account) => {
    state.account = account
  },
  SET_DEPART: (state, depart) => {
    state.depart = depart
  },
  SET_ID_DEPART: (state, id_depart) => {
    state.id_depart = id_depart
  },
  SET_ROLE: (state, role) => {
    state.role = role
  },
  SET_UID: (state, uid) => {
    state.uid = uid
  },
  SET_ID_ROLE: (state, id_role) => {
    state.id_role = id_role
  },
  SET_INDEX_COLUMN: (state, index_column) => {
    state.index_column = index_column
  }
}

const actions = {

  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },
  reqUserInfo({commit,state}){
      this.$axios.get('/api/user/info',{token:token}).then((res)=>{
        // commit("SET_USERINFO")
        console.log(res,666)
       })
  },

 // get user info
 getInfo({ commit, state }) {
  console.log({ commit, state });
  return new Promise((resolve, reject) => {
    getInfo(state.token).then(response => {
      let { data } = response
      // console.log(state)
      // data = {
      //   // roles: ['admin'],
      //   uid: 13,
      //   role: '管理员',
      //   id_role: 1,
      //   depart: '运营一部',
      //   name: '张三',
      //   account: 'admin@163.com'
      // }
      if (!data) {
        reject('Verification failed, please Login again.')
      }
      const { account, depart, name, role, uid, id_role, id_depart, index_column } = data
      commit('SET_NAME', name)
      commit('SET_ACCOUNT', account)
      commit('SET_DEPART', depart)
      commit('SET_ID_DEPART', id_depart)
      commit('SET_ROLE', role)
      // commit('SET_UID', uid)
      commit('SET_ID_ROLE', id_role)
      commit('SET_INDEX_COLUMN', index_column)
      resolve(data)

      console.log(data)
    }).catch(error => {
      reject(error)
    })
  })
},
  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        commit('SET_TOKEN', '')
        commit('SET_ROLES', [])
        removeToken()
        resetRouter()

        // reset visited views and cached views
        // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
        dispatch('tagsView/delAllViews', null, { root: true })

        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      commit('SET_ROLES', [])
      removeToken()
      resolve()
    })
  },

  // dynamically modify permissions
  async changeRoles({ commit, dispatch }, role) {
    const token = role + '-token'

    commit('SET_TOKEN', token)
    setToken(token)

    const { roles } = await dispatch('getInfo')

    resetRouter()

    // generate accessible routes map based on roles
    const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
    // dynamically add accessible routes
    router.addRoutes(accessRoutes)

    // reset visited views and cached views
    dispatch('tagsView/delAllViews', null, { root: true })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
